//重置样式
require('../../assets/css/index.less')

//引入weui库
require('../../assets/lib/weui/weui.css')
let weui = require('../../assets/lib/weui/weui.js')
console.log(weui);

//当前页面样式
require('./dynamic.less')

//引入字体库
require('../../assets/fonts/iconfont.css')

//引入dom
const dom = require('../../utils/dom.js')

//引入https
const https = require('../../utils/https.js')


//判定token是否放行
dom.getToken()

dom.reday(function () {

    //点击返回
    dom.getEvent('#comeBack',function(){
        history.go(-1)
    })

    //点击和添加图片联动
    dom.getEvent('#addImgBtn', function () {
        dom.getEle('#addImg').click()
    })

    //省份
    let proData;
    dom.getEvent('#proC', function () {
        //1.发ajax获取省份数据并拼接weui所需格式
        https.get('/api/shared/province', function (res) {
            //解构参数
            let { errno, data } = res;
            //判定
            if (errno == 0) {
                /*weuipicker格式为以下:
                    [{
                        label: '男',
                        value: 0
                    }]
                
                */
                //处理数据格式
                data = data.map(v => {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                //2.调用weui选择器
                weui.picker(data, {
                    // 选中触发事件
                    onConfirm: function (result) {
                        //把选中的值添加到网页中
                        dom.getEle('#proCp').innerHTML = result[0].label
                        //保存省份ID
                        proData = result[0].value

                    },
                    title: '请选择省份'
                });

            } else {
                weui.alert(res.message)
            }
        })

    })

    //城市
    let cityId;
    dom.getEvent('#cityC', function () {
        //判定选择了省份再进行下面代码
        if (proData) {
            //1.发ajax获取城市数据并拼接weui所需格式
            https.get(`/api/shared/city?provinceId=${proData}`, function (res) {
                //解构参数
                let { errno, data } = res;
                //判定
                if (errno == 0) {
                    //处理数据格式
                    data = data.map(v => {
                        return {
                            label: v.name,
                            value: v.id
                        }
                    })
                    //2.调用weui选择器
                    weui.picker(data, {
                        // 选中触发事件
                        onConfirm: function (result) {
                            //把选中的值添加到网页中
                            dom.getEle('#cityCp').innerHTML = result[0].label;
                            //保存城市id
                            cityId = result[0].value;

                        },
                        title: '请选择城市'
                    });

                } else {
                    weui.alert(res.message)
                }
            })
        }

    })

})